package org.example.file;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
@Slf4j
public class FileStorageService implements StorageService {

    private static final File BASE_DIR = new File("upload").getAbsoluteFile();

    @Override
    public void save(MultipartFile uploadedFile, String savePath) throws IOException {
        File destFile = getDestFile(savePath);
        File destDir = destFile.getParentFile();
        if (!destDir.exists()) {
            destDir.mkdirs();
        }
        log.info("saved: {}", destFile);
        uploadedFile.transferTo(destFile);
    }

    private static File getDestFile(String savePath) {
        return new File(BASE_DIR, savePath);
    }
}
